// pages/orderedia/orderedia.js
//读取下单数据， 提交订单 获取签名
import { getOrderConfirm, setOrderUp, setPlayOride } from '../../config/https'

import Toast from '../../miniprogram_npm/_vant-weapp@0.5.7@vant-weapp/toast/toast';


const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    stast:true,             //用于判断是否可以点击支付
    storesIndex: 0,

    show: false,                                                            //弹出框时间选择

    tiemvalue: '',                                                           //选择自提的时间
    inCall: '',                                                              //电话
    inName: '',                                                              //名字
    remark: '',                                                              //备注
    dispatcharr: null,                                                       //判断
    addreId: '',                                                             //地址id
    stores: null,                                                            //门店自提信息
    options: null,                                                          //读取上个页面传过来的数据  
    addressData: null,                                                      //地址信息默认地址
    vipType: '',                                                            //vip状态1是0不是
    shopList: null,                                                         //商品数据
    otherData: null,                                                        //商品信息统计
    status: true,                                                           //false地址不存在true存在
    radio:'0',                                                               //1门店自提0商家配送
    resSet:''
  },
  onConfirm(event) {
    const { picker, value, index } = event.detail;
    this.setData({
      tiemvalue: value[0] + value[1]
    })
    
  },

  onChange(event) {
    const { picker, value, index } = event.detail;
    picker.setColumnValues(1, citys[value[0]]);
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({options})
    console.log(options)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    // 读取vip
    wx.getStorage({
      key: 'vipStast',
      success: function (res) {
        that.setData({
          vipType: res.data
        })
      },
    })

    //读取其他页面过来的id
    var { id, contenner} = that.data.options
    //读取本地缓存是否存在地址id
    console.log(id, contenner)
    wx.getStorage({
      key: 'addreId',
      success: function (res) {
        var addreId = res.data;
        console.log(id, contenner+'订单数量1')
        getOrderConfirm({
          id: id,
          type: 'groups',
          addressid: addreId,
          num: contenner
        }).then(res => {
          console.log(res)
          res.other_data.total_price = Number(res.other_data.price) + Number(res.other_data.dispatch_price)
          let stores = JSON.stringify(res.stores)
          wx.setStorage({
            key: 'stores',
            data: stores,
          })
          that.setData({
            addressData: res.address,
            shopList: res.goods,
            otherData: res.other_data,
            stores: res.stores,
            dispatcharr: res.dispatch_array,
          })

        }).catch(err => {
          console.log(err)
          Toast(err.message)
          setTimeout(itme => {
            wx.navigateBack({
            })
          }, 2000)
        })
      }, fail() {
        console.log(id, contenner + '订单数量2')
        var addreId = ''
        getOrderConfirm({
          id: id,
          type:'groups',
          aid: addreId,
          num:contenner
        }).then(res => {
          console.log(res)
          //配送订单为0 不支持配送
          if (res.set.is_delivery == 0) {
            if (res.set.is_selflift == 1) {
              that.setData({ radio: '1' })
            } else {
              that.setData({ radio: '' })
            }
          } else if (res.set.is_delivery == 1) {
            that.setData({ radio: '0' })
          } 

          // if (res.set.is_delivery == 0) { //配送订单为0 不支持配送
          //   that.setData({ radio:' ' })
          // } else if (res.set.is_delivery == 1) { //配送订单为1 支持配送
          //   that.setData({ radio:'0' })
          // }
          // if (res.set.is_selflift == 0) { //自提订单为0 不支持配送
          //   that.setData({ radio: ' ' })
          // } else if (res.set.is_selflift == 1) { //自提订单为1 支持配送
          //   that.setData({ radio:'1' })
          // }

          res.other_data.total_price = Number(res.other_data.price) + Number(res.other_data.dispatch_price)
          let stores = JSON.stringify(res.stores)
          wx.setStorage({
            key: 'stores',
            data: stores,
          })
          that.setData({
            addressData: res.address,
            shopList: res.goods,
            otherData: res.other_data,
            stores: res.stores,
            dispatcharr: res.dispatch_array,
            tiemvalue:res.goods.pickup_time,
            resSet: res.set
          })
          }).catch(err => {
           
          Toast(err.message)
          setTimeout(itme=>{
            wx.navigateBack({
              delta: 1
            })
          },2000)
        })
      }
    })
  },


  // 单选框切换
  onChanges(e) {
    console.log(e)
    let radio = Number(e.detail)
    this.setData({ radio })
  },
  // 监听买家留言输入事件
  onInput(e) {
    this.setData({
      remark: e.detail.value
    })
  },
  onName(e) {
    this.setData({
      inName: e.detail.value
    })
  },
  onCall(e) {
    this.setData({
      inCall: e.detail.value
    })
  },

  //点击微信支付
  paymenStatus() {
    console.log("hhh")
    
    var addressData = this.data.addressData //地址id
    var tiemvalue = this.data.tiemvalue     //自提时间
    var radio = this.data.radio             //1自提0商家配送
    var stores = this.data.stores           //门店自提信息
    var remark = this.data.remark;          //留言
    if (!this.data.stast) {
      Toast("当前有订单正在进行,请稍后再试~~")
      return
    }
    if (!addressData.id && radio == 0) {
      Toast("请选择收货地址~~")
      return
    }
    // if (this.data.dispatcharr.city_express_state==0 && radio == 0) {
    //   Toast("当前地址不支持配送~~")
    //   return
    // }
    if (!this.data.tiemvalue && radio == 1) {
      Toast("请选择自提时间~~")
      return
    }
    if (!this.data.inName && radio == 1) {
      Toast("请输入联系人~~")
      return
    }
    if (!this.data.inCall && radio == 1) {
      Toast("请输入联系人电话~~")
      return
    }
    //提交订单
    var that = this
    console.log(that.data.options.contenner)
    setOrderUp({
      id: that.data.options.id,                                //商品id
      type: 'groups',                                          //固定值
      mobile: radio == 1 ? that.data.inCall : '',               //电话
      realname: radio == 1 ? that.data.inName : '',             //姓名
      aid: radio == 0 ? addressData.id : '',                    //地址
      carrierid: radio == 1 ? stores[that.data.storesIndex].id : '',
      dispatchtype: radio,                                     //0物流1自提
      message: remark,                                          //备注
      num:that.data.options.contenner                        //购买的数量
    }).then(res => {
      that.setData({
        stast: false
      })
      that.palyPick(res.orderid, res.teamid)
    }).catch(err => {
      Toast(err.message)
    })
   
  },
  // 微信支付接口
  palyPick(id, teamid) {
    setPlayOride({
      orderid: id,
      teamid: teamid
    }).then(res => {
      console.log(res)
      var payInfo = res.wechat
      //微信支付
      wx.requestPayment({
        'timeStamp': payInfo.timeStamp,
        'nonceStr': payInfo.nonceStr,
        'package': payInfo.package,
        'signType': 'MD5',
        'paySign': payInfo.paySign,
        'success': res => {
          console.log(res)
          //type0成功1失败
          wx.redirectTo({ 
            url: '/pages/paymenstapur/paymenstapur?type=0&orderid=' + id + '&teamid=' + teamid,
          })
        },
        'fail': err => {
          wx.redirectTo({
            url: '/pages/paymensta/paymensta?type=1',
          })
          // Toast('支付失败请前往订单页支付~~')
          // wx.redirectTo({
          //   url: '/pages/paymenstapur/paymenstapur?type=1&orderid=' + id + '&teamid=' + teamid,
          // })
        }
      })
    })

  },
  // 时间变换
  onChangeTime(event) {
    const { picker, value, index } = event.detail;
    picker.setColumnValues(1, citys[value[0]]);
  },
  // 点击跳转收货地址
  onAdder() {
    wx.navigateTo({
      url: '/pages/myaddress/myaddress?stash=1',
    })
  
  },
  //点击跳转附近门店
  onNearby() {
    wx.navigateTo({
      url: '/pages/nearbystore/nearbystore',
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
 
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})